package com.yt.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@Slf4j
public class SendtinelController {
    @Value("${info}")
    String info;

    @GetMapping("/param")
    //http://localhost:8080/param?b=2&a=1
    //参数索引指的是方法当中参数的位置,高级选项指的是值，blockHandler处理在面板上配置的异常问题
    @SentinelResource(value = "param",blockHandler="getParamHandler",fallback = "getParamFallback")
    public String getParam(@RequestParam(name = "a",required = false) String a,
                           @RequestParam(name = "b",required = false) String b){
        int a_i=Integer.parseInt(a);
        int i=30/a_i;
        return "得到消息"+a+b;
    }
    public String getParamHandler(@RequestParam(name = "a",required = false) String a,
                                  @RequestParam(name = "b",required = false) String b
                            , BlockException e){
        log.info(e.toString());
        return "得到消息"+a+b+"异常处理方式";
    }

    public String getParamFallback( String a,  String b ){
        return "其它java异常处理方式";
    }
    @RequestMapping("/getinfo")
    public String getInfo() {
       String s=null;
       s.split(".");
        try {
            Thread.sleep(900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return info;
    }

    @RequestMapping("/getid")
    public String getId() {
        return UUID.randomUUID().toString();
    }

}